package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     */
    @CacheEvict(cacheNames = "setmeal",
                allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 套餐分页查询
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo1 = new Page<>(page, pageSize);
        QueryWrapper<Setmeal> queryWrap = new QueryWrapper<>();
        queryWrap.like(StringUtils.isNotBlank(name), "name", name).orderByDesc("update_time");
        setmealService.page(pageInfo1, queryWrap);

        Page<SetmealDto> pageInfo2 = new Page<>();
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal record : pageInfo1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);

            //TODO 设置分类名称
            Category category = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        pageInfo2.setRecords(setmealDtoList);
        return R.success(pageInfo2);
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> deleteByIds(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 套餐查询
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "setmeal",
               key = "#setmeal.categoryId+ '_'+ #setmeal.status",
               unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", setmeal.getCategoryId())
                .eq("status", setmeal.getStatus())
                .orderByDesc("update_time");
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return R.success(setmealList);
    }

    /**
     * 点击图片操作
     */
    @GetMapping("dish/{id}")
    public R<List<Setmeal>> dish(@PathVariable Long id) {
        QueryWrapper<Setmeal> queryWrap = new QueryWrapper<>();
        queryWrap.eq("id", id);
        List<Setmeal> list = setmealService.list(queryWrap);
        return R.success(list);
    }

    /**
     * 修改订单状态
    public R<Setmeal> status(@PathVariable Integer status,List<Long> ids){
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();

        return R.success(null);
    }*/
}

